package com.rainng.coursesystem.service;

import com.rainng.coursesystem.dao.mapper.PostMapper;
import com.rainng.coursesystem.repository.PostEntity;
import com.rainng.coursesystem.repository.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rainng.coursesystem.repository.PostRes;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class PostService {

    @Autowired
    private PostRepository postRepository;
    @Autowired
    private PostMapper postMapper;
    @Autowired
    private PostRes postRes;

    // 根据课程ID获取所有帖子，并附加点赞状态和收藏状态
    public List<Map<String, Object>> getPostsWithLikeAndFavoriteStatus(int courseId, int userId) {
        return postRes.getPostsWithLikeAndFavoriteStatus(courseId, userId);
    }
    // 判断帖子是否存在
    public boolean isPostExist(int postId) {
        return postRepository.existsById(postId);
    }

    // 创建新帖子
    public PostEntity createPost(PostEntity post) {
        post.setCreatedAt(LocalDateTime.now());
        postMapper.createPost(post);
        return post;
    }

    // 删除帖子
    public void deletePost(int postId) {
        postMapper.deletePost(postId);
    }

    // 更新帖子
    public void updatePost(int postId, PostEntity post) {
        post.setPostId(postId);
        postMapper.updatePost(post);
    }

    // 点赞帖子（记录userId）
    public void likePost(int userId, int postId) {
        if (!postRes.isPostLikedByUser(userId, postId)) {
            postRes.likePost(userId, postId);
        } else {
            throw new RuntimeException("Post already liked by user");
        }
    }

    // 取消点赞帖子
    public void unlikePost(int userId, int postId) {
        if (postRes.isPostLikedByUser(userId, postId)) {
            postRes.unlikePost(userId, postId);
        } else {
            throw new RuntimeException("Post not liked by user");
        }
    }

    // 获取帖子并附加点赞状态
    public Map<String, Object> getPostWithLikeStatus(int postId, int userId) {
        Map<String, Object> postDetails = postRes.getPostById(postId);
        boolean isLiked = postRes.isPostLikedByUser(userId, postId);
        postDetails.put("isLiked", isLiked);
        return postDetails;
    }

    // 根据关键词搜索帖子和评论
    public List<Map<String, Object>> searchByKeywordAndCourseId(String keyword, int courseId) {
        return postRes.searchByKeywordAndCourseId(keyword, courseId);
    }

    // 收藏帖子
    public void favoritePost(int userId, int postId) {
        if (!postRes.isFavorite(userId, postId)) {
            postRes.addFavorite(userId, postId);
        } else {
            throw new RuntimeException("Post already favorited");
        }
    }

    // 取消收藏帖子
    public void unfavoritePost(int userId, int postId) {
        if (postRes.isFavorite(userId, postId)) {
            postRes.removeFavorite(userId, postId);
        } else {
            throw new RuntimeException("Post not favorited");
        }
    }

    public List<Map<String, Object>> getFavoritesWithStatusByUserId(int userId) {
        return postRes.getFavoritePostsWithStatus(userId);
    }


    // 获取课程下所有帖子并附加点赞和收藏状态
    public List<Map<String, Object>> getPostsByCourseAndUser(int courseId, int userId) {
        return postRes.getPostsWithLikeAndFavoriteStatus(courseId, userId);
    }
}
